package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiServerUserData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiUserMallData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SonHaiServerUserDataDaoImpl extends TempDataObjectDao<SonHaiServerUserData> {


    public static String getSonHaiServerUserDataSql = "select * from `t_u_activity_son_hai_user_data` where `activityId`=? and `userId`=?";
    public SonHaiServerUserData getSonHaiServerUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSonHaiServerUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSonHaiServerUserDataSql = "update `t_u_activity_son_hai_user_data` set mallDataMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SonHaiServerUserData sonHaiServerUserData) {
        sonHaiServerUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiServerUserData.getMallDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiServerUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiServerUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSonHaiServerUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiServerUserDataSql = "insert into `t_u_activity_son_hai_user_data`(activityId,userId,mallDataMap) values (?,?,?)";
    @Override
    protected boolean addData(SonHaiServerUserData sonHaiServerUserData) {
        sonHaiServerUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiServerUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiServerUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiServerUserData.getMallDataMap())));
        if (getDb().executeNoneQuery(insertSonHaiServerUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiServerUserData parseData(ResultSet rs) throws SQLException {
        SonHaiServerUserData sonHaiServerUserData = new SonHaiServerUserData();
        sonHaiServerUserData.setActivityId(rs.getInt("activityId"));
        sonHaiServerUserData.setUserId(rs.getLong("userId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("mallDataMap"))) {
            sonHaiServerUserData.setMallDataMap(JSON.parseObject(rs.getString("mallDataMap"), new TypeReference<Map<Integer, SonHaiUserMallData>>() {}));
        }
        return sonHaiServerUserData;
    }

}
